﻿
using MX.Core.Http.WebAction;
using MX.Core.Log;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MX.Core.Assets
{
    public class FileUploader
    {

        static public int TryCount { get; set; } = 3;
        static ILogger log = Logger.GetLogger("文件上传");
        public class Host : WebAction
        {

            public UploadResponse Upload(string resHost, string fileName, byte[] fileData, string siteName)
            {
                var response = new UploadResponse() { code = ReponseCode.ClientError, url = "" };
                for (int i = 0; i < TryCount; i++)
                {
                    try
                    {
                        if (!resHost.EndsWith("/")) resHost = resHost + "/";
                        string ResourcesUrl = resHost + "HttpHandle/upload";
                        BoundaryPostInfo postInfo = new BoundaryPostInfo();
                        postInfo.AddFile(fileName, fileData);
                        postInfo.AddPars("SiteName", siteName);
                        postInfo.AddPars("OrgFileName", fileName);
                        log.Debug(fileName + "---" + siteName + "---" + fileData.Length);
                        var json = GetWebData<string>(ResourcesUrl, postInfo: postInfo, timeOut: 60 * 60 * 1000);
                        log.Debug(json);
                        response = JsonConvert.DeserializeObject<UploadResponse>(json);
                        break;
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex, "");
                        if (i == TryCount - 1)
                        {
                            response = new UploadResponse() { code = ReponseCode.ClientError, url = "" };
                        }
                        else
                        {
                            Thread.Sleep(1000 * i);
                            continue;
                        }
                    }
                }
                return response;
            }

        }

        static Host host = new Host();

        public static UploadResponse Upload(string resHost, string filePath)
        {
            if (!File.Exists(filePath))
            {
                log.Error($"本地文件不存在{filePath}");
                return new UploadResponse() { code = ReponseCode.ClientError, url = "本地文件不存在" };
            }
            try
            {
                var data = File.ReadAllBytes(filePath);
                return Upload(resHost, Path.GetFileName(filePath), data);
            }
            catch (Exception)
            {
                log.Error($"本地文件读取失败{filePath}");
                return new UploadResponse() { code = ReponseCode.ClientError, url = "本地文件读取失败" };
            }

        }

        public static UploadResponse Upload(string resHost, string fileName, byte[] fileData)
        {
            return Upload(resHost, fileName, fileData, "MX");
        }
        public static UploadResponse Upload(string resHost, string fileName, byte[] fileData, string siteName)
        {

            return host.Upload(resHost, fileName, fileData, siteName);
        }

    }
}
